package com.dlyk.web;

import com.alibaba.excel.EasyExcel;
import com.dlyk.constant.Constants;
import com.dlyk.model.TClue;
import com.dlyk.model.TCustomer;
import com.dlyk.query.CustomerQuery;
import com.dlyk.result.CustomerExcel;
import com.dlyk.result.R;
import com.dlyk.service.CustomerService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.xmlbeans.impl.xb.xsdschema.LocalSimpleType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/29 22:33:59
 * @version: 1.0
 */
@RestController
public class CustomerController {

    @Resource
    private CustomerService customerService;


    /**
     * @description: 新增客户
     * @params: [customerQuery, token]
     * @return:
     * @author: ajie
     * @date: 2024/7/31 22:05
     */
    @PostMapping("/api/clue/customer")
    public R convertCustomer(@RequestBody CustomerQuery customerQuery,@RequestHeader(value = "Authorization") String token){
        customerQuery.setToken(token);
        Boolean convert = customerService.convertCustomer(customerQuery);
        return convert ? R.OK() : R.FAIL();
    }

    /**
     * @description: 获取客户信息列表
     * @params: [current]
     * @return:
     * @author: ajie
     * @date: 2024/8/1 18:28
     */
    @GetMapping("api/customers")
    public R cluePage(@RequestParam(value = "current",required = false) Integer current,CustomerQuery customerQuery){
        if (current == null){
            current = 1;
        }
        PageInfo<TCustomer> pageInfo = customerService.getCustomerByPage(current,customerQuery);
        return R.OK(pageInfo);
    }

    @GetMapping("api/exportExcel")
    private void exportExcel(HttpServletResponse response,@RequestParam(value = "ids",required = false) String ids) throws IOException {
        //想要让浏览器弹出下载框，后端需要设置一下请求头信息
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(Constants.EXCEL_FILE_NAME+System.currentTimeMillis(), StandardCharsets.UTF_8) +".xlsx");

        //后端查询数据库的数据，把数据写入Excel。然后把Excel以IO流的方式输出到前端浏览器
        List<String> idList = StringUtils.hasText(ids) ? Arrays.asList(ids.split(",")) : new ArrayList<>();
        List<CustomerExcel> dataList = customerService.getCustomerByExcel(idList);
        //第一个参数是输出流，第二个参数模板流，第三个参数是excel中的sheet，第四个参数是查到的数据
        EasyExcel.write(response.getOutputStream(), CustomerExcel.class)
                .sheet()
                .doWrite(dataList);

    }

    /**
     * 根据id查询客户详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/api/customer/detail/{id}")
    public R clueDetail(@PathVariable(value = "id") Integer id) {
        TCustomer tCustomer = customerService.getCustomerById(id);

        return R.OK(tCustomer);
    }


}
